package dao.impl;

import dao.UserDao;
import entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;
import resultsetextractor.UserResultSetExtractor;
import rowmapper.UserRowMapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

@Repository("userDao3")
public class UserDaoImpl3 implements UserDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public void insert(User user) {
        String sql=new StringBuffer()
                .append("insert into t_user ")
                .append("    (username,password) ")
                .append("values ")
                .append("    (?,?) ")
                .toString();
        jdbcTemplate.update(sql,user.getUsername(),user.getPassword());
    }

    @Override
    public void deleteById(Integer id) {
        String sql=new StringBuffer()
                .append("delete from t_user ")
                .append("where id=? ")
                .toString();
        jdbcTemplate.update(sql,id);
    }

    @Override
    public void update(User user) {
        String sql=new StringBuffer()
                .append("update t_user ")
                .append("set password=? ")
                .append("where id=? ")
                .toString();
        jdbcTemplate.update(sql,user.getPassword(),user.getId());
    }

    @Override
    public List<User> selectAll() {
        String sql=new StringBuffer()
                .append("select * ")
                .append("from t_user ")
                .toString();
        return jdbcTemplate.query(sql,new UserRowMapper());
    }

    @Override
    public User selectById(Integer id) {
        String sql=new StringBuffer()
                .append("select * ")
                .append("from t_user ")
                .append("where id=? ")
                .toString();
        return jdbcTemplate.query(sql,new UserResultSetExtractor(),id);
    }

    @Override
    public Integer save(User user) {
        String sql=new StringBuffer()
                .append("insert into t_user ")
                .append("    (username,password) ")
                .append("values ")
                .append("    (?,?) ")
                .toString();
        KeyHolder keyHolder=new GeneratedKeyHolder();
        jdbcTemplate.update(new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
                PreparedStatement ps=con.prepareStatement(sql,PreparedStatement.RETURN_GENERATED_KEYS);
                ps.setString(1,user.getUsername());
                ps.setString(2,user.getPassword());
                return ps;
            }
        },keyHolder);

        return ((Long) keyHolder.getKey()).intValue();
    }

    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
}
